package net.melon.sysres.provider.impl;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;

import net.esj.basic.core.engine.util.enumeration.EnumVo;
import net.esj.basic.pojo.EnumStructure;
import net.esj.basic.pojo.TreeStructure;
import net.melon.sysres.manager.ResourceManager;
import net.melon.sysres.pojo.Resource;
import net.melon.sysres.provider.ResourceProvider;

@Singleton
@Named("sysres.resourceProvider")
public class ResourceProviderImpl implements ResourceProvider {

	private ResourceManager resourceManager;
	
	@Override
	public List<EnumStructure> findEnum(String key) {
		List list = resourceManager.loadResourcesByType(key,0,true);
		if(list.isEmpty()){
			return Collections.EMPTY_LIST;
		}
		return list;
	}

	@Override
	public List<EnumVo> findEnumVo(String key) {
		List list = findEnum(key);
		if(list.isEmpty()){
			return Collections.EMPTY_LIST;
		}
		List<EnumVo> ret = EnumVo.convertToEnumVo(list);
		return ret;
	}

	@Override
	public EnumStructure getEnum(String key, String code) {
		Resource resource = resourceManager.findResourceByTypeAndValue(key, code);
		return resource;
	}

	@Override
	public EnumVo getEnumVo(String key, String code) {
		EnumStructure e = getEnum(key, code);
		if(e!=null){
			EnumVo vo = EnumVo.convertToEnumVo(e);
			return vo;
		}
		return EnumVo.EMPTY;
	}

	@Override
	public List<TreeStructure> loadChildren(TreeStructure parent) {
		List list = resourceManager.loadResources((Resource) parent);
		return list;
	}

	@Override
	public List<TreeStructure> findTree(String type) {
		List list = resourceManager.loadResourcesByType(type, 0,true);
		return list;
	}

	@Override
	public TreeStructure getTree(String type, Serializable id) {
		Resource resource = resourceManager.getResource(id.toString());
		return resource;
	}

	@Inject
	public void setResourceManager(ResourceManager resourceManager) {
		this.resourceManager = resourceManager;
	}

	@Override
	public int getOrder() {
		return 0;
	}

}
